package org.airline.impl;

import java.util.List;

import org.airline.dao.IFlightDao;
import org.airline.entity.Flight;
import org.airline.entity.PageBean;
import org.springframework.stereotype.Component;

@Component("FlightDaoImpl")
@SuppressWarnings("all")
public class FlightDaoImpl extends HibernateDaoImpl implements IFlightDao{

	@Override
	public void deleteFlightById(int id) throws Exception {
		this.delete(Flight.class, id);
	}

	@Override
	public List<Flight> getAllFlight() throws Exception {
		return (List<Flight>)super.find("FROM Flight");
	}

	@Override
	public Flight getAllFlightById(int id) throws Exception {
		return (Flight)super.get(Flight.class, id);
	}

	@Override
	public boolean insertFlight(Flight flight) throws Exception {
		super.getHibernateTemplate().save(flight);
		return true;
	}

	@Override
	public void updateFlight(Flight flight) throws Exception {
		super.update(flight);
	}

	@Override
	public PageBean queryForPage(int pageSize, int page) throws Exception {
		final String hql = "FROM Flight";
		return super.queryForPage(hql, pageSize, page);
	}
	//全文搜索
	@Override
	public List<Flight> getFlightsByWords(String keyWord) throws Exception {
		String hql = "FROM Flight" 
			+ "WHERE flight_name LIKE '%" + keyWord + "%' "
			+ "OR flight_type LIKE '%" + keyWord + "%' "
			+ "OR capacity LIKE '%" + keyWord + "%' "
			+ "OR start_flight LIKE '%" + keyWord + "%' "
			+ "OR end_flight LIKE '%" + keyWord + "%' "
			+ "OR times LIKE '%" + keyWord + "%' "
			+ "OR price LIKE '%" + keyWord + "%' ";
		return (List<Flight>)super.getHibernateTemplate().find(hql);
	}
	
	//根据关键字分页查询
	@Override
	public PageBean queryForPageByKeyWords(String keyWord, int pageSize,
			int page) throws Exception {
		String hql = "FROM Flight "
			+ "WHERE flight_name LIKE '%" + keyWord + "%' "
			+ "OR flight_type LIKE '%" + keyWord + "%' "
			+ "OR capacity LIKE '%" + keyWord + "%' "
			+ "OR start_flight LIKE '%" + keyWord + "%' "
			+ "OR end_flight LIKE '%" + keyWord + "%' "
			+ "OR times LIKE '%" + keyWord + "%' "
			+ "OR price LIKE '%" + keyWord + "%' ";
	return super.queryForPage(hql, pageSize, page);
	}



}
